/*
 * @Author: Laputa
 * @Version: V0.1
 * @Date: 2023-06-12 10:47:45
 * @LastEditors: Laputa
 * @LastEditTime: 2023-10-13 15:05:05
 * @Description:This file provides firmware functions to manage the following
 *              functionalities of the PIT part.
 * 
 *              PIT default initialize
 *              PIT initialize
 *              PIT set timer value
 *              PIT enable and disable 
 *              PIT clear interrupt flag
 *
 * Copyright (c) 2023 by Levetop, All Rights Reserved.
 */

/*
 *  PIT_Prescaler = 2^PRE;
 *  PIT_modulus   = PMR;
 *  PIT timing time equal to (PIT_Prescaler * PIT_modulus)/system clock.
 */

#include "lt168_pit.h"

/**
 * @brief Sets default values for the pit initialization structure variable pointer.
 * @param {pit_config_ts *} pit_config: Pointer to a pit initialization structure that
 *                          contains the configuration information for the specified
 *                          PIT peripheral.
 * @return {void}
 */
void PIT_GetDefault(pit_config_ts *pit_config)
{
    pit_config->pmr = 0xFFFFU;
    pit_config->prescaler = PIT_Pre_1;
    pit_config->pit_cs.pdmae = Disable;
    pit_config->pit_cs.pdoze = false;
    pit_config->pit_cs.pdbg  = false;
    pit_config->pit_cs.ovm   = PIT_Ovm_0x0000;
    pit_config->pit_cs.pie   = Disable;
    pit_config->pit_cs.pif   = true;
    pit_config->pit_cs.rld   = PIT_Rld_0xFFFF;
    pit_config->pit_cs.en    = Disable;
}

/**
 * @brief Initializes the PIT peripheral according to the specified parameters in the Pit_Init.
 * @param {pit *} pitx:Pointer to a pit structure,where can be PIT0,PIT1,PIT2,PIT3.
 * @param {pit_config_ts *} pit_config: Pointer to a pit initialization structure that
 *                          contains the configuration information for the specified
 *                          PIT peripheral.
 * @return {void}
 */
void PIT_Init(pit *pitx, pit_config_ts *pit_config)
{
    uint16_t tempReg   = 0U;
    pitx->PCSR         = 0U;

    tempReg    = PIT_PCSR_PRE(pit_config->prescaler) | PIT_PCSR_PDMAE(pit_config->pit_cs.pdmae) |
                 PIT_PCSR_OVW(pit_config->pit_cs.ovm) | PIT_PCSR_RLD(pit_config->pit_cs.rld) | 
                 PIT_PCSR_EN(pit_config->pit_cs.en);

    if (pit_config->pit_cs.pie == Enable)
        tempReg |= PIT_PCSR_PIE(pit_config->pit_cs.pie) | PIT_PCSR_PIF(true);

    pitx->PCSR = tempReg;

    pitx->PMR = pit_config->pmr;
}

/**
 * @brief Write PIT modulus
 * @param {pit *} pitx:Pointer to a pit structure,where can be PIT0,PIT1,PIT2,PIT3.
 * @param {uint16_t} pmr: PIT modulus,The initial value of the PIT downward count.
 * @return {void}
 */
void PIT_Write_PMR(pit *pitx, uint16_t pmr)
{
    pitx->PMR = pmr;
}

/**
 * @brief Get PIT Count Value
 * @param {pit *} pitx:Pointer to a pit structure,where can be PIT0,PIT1,PIT2,PIT3.
 * @return {uint16_t}
 */
uint16_t PIT_Get_Count_Value(pit *pitx)
{
    return pitx->PCNTR;
}

/**
 * @brief Enables the specified PIT peripheral.
 * @param {pit *} pitx:Pointer to a pit structure,where can be PIT0,PIT1,PIT2,PIT3.
 * @return {void}
 */
void PIT_Enable(pit *pitx)
{
    Bit_Set(pitx->PCSR, PIT_PCSR_EN_MASK);
}

/**
 * @brief Disables the specified PIT peripheral.
 * @param {pit *} pitx:Pointer to a pit structure,where can be PIT0,PIT1,PIT2,PIT3.
 * @return {void}
 */
void PIT_Disable(pit *pitx)
{
    Bit_Clear(pitx->PCSR, PIT_PCSR_EN_MASK);
}

/**
 * @brief Clear PIT Interrupt Flag.
 * @param {pit *} pitx:Pointer to a pit structure,where can be PIT0,PIT1,PIT2,PIT3.
 * @return {void}
 */
void PIT_Clear_IntFlag(pit *pitx)
{
    Bit_Set(pitx->PCSR, PIT_PCSR_PIF_MASK);
}

